package cn.com.zzl.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import java.util.List;

/**
 * 该工具类的特点是在把pojo转换成json数据的时候可以有筛选有目的的进行转换不需要的属性可以排除掉！
 * 而且不涉及JSONObject以及JSONArray的操作。
 * Description：JSON工具类（net.sf.json-lib.json-lib）
 * Created by 朱正磊 on 2017-05-23 15:29.
 */
public class JSONUtil {

	/**
	 * @Desc 将List对象序列化为JSONArray
	 * @param list
	 * @param excludes
	 * @param <T>
	 * @return
	 */
	public static <T> JSONArray list2JsonArray(List<T> list, String[] excludes){
		if(excludes != null){
			JsonConfig config = new JsonConfig();
			config.setExcludes(excludes);
			return JSONArray.fromObject(list,config);
		}
		return JSONArray.fromObject(list);
	}

	/**
	 * 说明：该方法可有可无！
	 * @Desc 将List对象序列化为JSONArray字符串
	 * @param list
	 * @param excludes
	 * @param <T>
	 * @return
	 */
	public static <T> String list2JsonArrayString(List<T> list, String[] excludes){
		return list2JsonArray(list, excludes).toString();
	}

	/**
	 * @Desc 将Object对象序列化为JSONObject
	 * 		 说明：该方法只适用于javaBean，Integer、String等不适用！
	 * @param object
	 * @return
	 */
	public static JSONObject object2JsonObject(Object object, String[] excludes){
		if(excludes != null){
			JsonConfig config = new JsonConfig();
			config.setExcludes(excludes);
			return JSONObject.fromObject(object,config);
		}
		return JSONObject.fromObject(object);
	}

	/**
	 * 说明：该方法可有可无！
	 * @Desc 将Object对象序列化为JSONObject字符串
	 *       说明：该方法只适用于javaBean，Integer、String等不适用！
	 * @param object
	 * @return
	 */
	public static String object2JsonObjectString(Object object, String[] excludes){
		return object2JsonObject(object, excludes).toString();
	}

	/**
	 * @Desc 测试JSONUtil
	 * @param args
	 */
	/*public static void main(String[] args) {

		User user1 = new User();
		user1.setSex("男");
		user1.setUsername("朱正磊");

		User user2 = new User();
		user2.setSex("女");
		user2.setUsername("翟甜");

		List<User> list = new ArrayList<User>();
		list.add(user1);
		list.add(user2);

		String[] excludes = {"id","birthday","address"};
		JSONArray jsonArray = JSONUtil.list2JsonArray(list,excludes);
		System.out.println(jsonArray);//[{"sex":"男","username":"朱正磊"},{"sex":"女","username":"翟甜"}]
		String jsonArrayString = JSONUtil.list2JsonArrayString(list,excludes);
		System.out.println(jsonArrayString);//[{"sex":"男","username":"朱正磊"},{"sex":"女","username":"翟甜"}]

		JSONArray jsonArray2 = JSONUtil.list2JsonArray(list,null);
		System.out.println(jsonArray2);//[{"address":"","birthday":null,"id":0,"sex":"男","username":"朱正磊"},{"address":"","birthday":null,"id":0,"sex":"女","username":"翟甜"}]
		String jsonArrayString2 = JSONUtil.list2JsonArrayString(list,null);
		System.out.println(jsonArrayString2);//[{"address":"","birthday":null,"id":0,"sex":"男","username":"朱正磊"},{"address":"","birthday":null,"id":0,"sex":"女","username":"翟甜"}]

		JSONObject jsonObject = JSONUtil.object2JsonObject(user1,excludes);
		System.out.println(jsonObject);//{"sex":"男","username":"朱正磊"}
		String jsonObjectString = JSONUtil.object2JsonObjectString(user1,excludes);
		System.out.println(jsonObjectString);//{"sex":"男","username":"朱正磊"}

		JSONObject jsonObject2 = JSONUtil.object2JsonObject(user1,null);
		System.out.println(jsonObject2);//{"address":"","birthday":null,"id":0,"sex":"男","username":"朱正磊"}
		String jsonObjectString2 = JSONUtil.object2JsonObjectString(user1,null);
		System.out.println(jsonObjectString2);//{"address":"","birthday":null,"id":0,"sex":"男","username":"朱正磊"}

	}*/

	/**
	 * @Desc 测试JSONUtil
	 * @param args
	 */
	/*public static void main(String[] args) {

		List<Integer> list1 = new ArrayList<Integer>();
		list1.add(1);
		list1.add(2);
		list1.add(3);
		list1.add(4);
		list1.add(5);
		list1.add(6);

		List<String> list2 = new ArrayList<String>();
		list2.add("a");
		list2.add("b");
		list2.add("c");
		list2.add("d");
		list2.add("e");
		list2.add("f");

		List<User> list3 = new ArrayList<User>();
		User user1 = new User();
		user1.setUsername("zzl");
		user1.setSex("男");
		User user2 = new User();
		user1.setUsername("zt");
		user1.setSex("女");
		list3.add(user1);
		list3.add(user2);

		System.out.println(JSONUtil.list2JsonArray(list1,null));//[1,2,3,4,5,6]
		System.out.println(JSONUtil.list2JsonArray(list2,null));//["a","b","c","d","e","f"]
		System.out.println(JSONUtil.list2JsonArray(list3,null));//[{"address":"","birthday":null,"id":0,"sex":"女","username":"zt"},{"address":"","birthday":null,"id":0,"sex":"","username":""}]

		System.out.println(JSONUtil.list2JsonArrayString(list1,null));//[1,2,3,4,5,6]
		System.out.println(JSONUtil.list2JsonArrayString(list2,null));//["a","b","c","d","e","f"]
		System.out.println(JSONUtil.list2JsonArrayString(list3,null));//[{"address":"","birthday":null,"id":0,"sex":"女","username":"zt"},{"address":"","birthday":null,"id":0,"sex":"","username":""}]

	}*/
	
}
